package interpret.demo3;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Hongliang Zhu
 * @create 2023-01-08 13:45
 */
public class AndExpression implements Expression {
    private List<Expression> expressions = new ArrayList<>();

    public AndExpression(String strAndExp) {
        String[] strs = strAndExp.split("&&");
        for (String str : strs) {
            if (str.contains(">")) {
                expressions.add(new GreaterExpression(str));
            } else if (str.contains("<")) {
                expressions.add(new LessExpression(str));
            } else if (str.contains("==")) {
                expressions.add(new EqualExpression(str));
            } else {
                throw new RuntimeException("Expression is invalid: " + str);
            }
        }
    }

    public AndExpression(List expressions) {
        this.expressions.addAll(expressions);
    }

    @Override
    public boolean interpret(Map<String, Long> stats) {
        for (Expression expr : expressions) {
            if (!expr.interpret(stats)) {
                return false;
            }
        }
        return true;
    }
}
